var sumPageCount;
var locationCurrPage = 1;
var currClass = "all";
var child = false;
var currParentTitle;
var currTeacherId;

function showPage() {
    showVideoByPageNumber(1);
    showNumber();
}
function showNumber() {
    if (sumPageCount !== 0) {
        var str = "<li><a onclick=\"jumpToPreviousPage()\">&laquo;</a></li>";
        for (var i = 0; i < sumPageCount; i++) {
            str += "<li><a onclick=\"showVideoByPageNumber(" + (i + 1) + ")\">" + (i + 1) + "</a></li>";
        }
        str += "<li><a onclick=\"jumpToNextPage()\">&raquo;</a></li>";
        document.getElementById("page-number").innerHTML = str;
    }
}

function jumpToPreviousPage() {
    if (child === false) {
        if (locationCurrPage !== 1) {
            locationCurrPage = locationCurrPage - 1;
            showVideoByPageNumber(locationCurrPage);
        }
    } else {
        if (locationCurrPage !== 1) {
            locationCurrPage = locationCurrPage - 1;
            showChildVideo();
        }
    }

}
function jumpToNextPage() {
    if (child === false) {
        if (locationCurrPage !== sumPageCount) {
            locationCurrPage = locationCurrPage + 1;
            showVideoByPageNumber(locationCurrPage);
        }
    } else {
        if (locationCurrPage !== sumPageCount) {
            locationCurrPage = locationCurrPage + 1;
            showChildVideo();
        }
    }

}
function showVideoByPageNumber(pageNumber) {
    locationCurrPage = pageNumber;
    if(child===false){
        if (currClass === "all") {
            var URL = "/getParentVideo?pageNumber=" + pageNumber;
            $.ajax({
                type: 'GET',
                url: URL,
                async: false,
                success: showVideoCoursesSuccess,
                error: function() {
                    alert("ajaxError: 函数showVideoByPageNumber请求失败");
                }
            });
        } else {
            var URL = "/getParentVideo?pageNumber=" + pageNumber + "&videoClass=" + currClass;
            $.ajax({
                type: 'GET',
                url: URL,
                async: false,
                success: showVideoCoursesSuccess,
                error: function() {
                    alert("ajaxError: 函数showVideoByPageNumber请求失败");
                }
            });
        }
    }
    showChildVideo();
}

function showVideoCoursesSuccess(data) {
    var videoInfo = JSON.parse(data);
    var result = videoInfo.result;

    if (result === "error: 没有视频合集") {
        document.getElementById("coursesInformation").innerHTML = "该类视频暂无";
        document.getElementById("page-number").innerHTML = "";
        document.getElementById("locationPageNumber").innerHTML = "";
    } else {
        var videoNumber = videoInfo.parentList.length;
        var str = "";
        for (var i = 0; i < videoNumber; i++) {
            var parenTitle = videoInfo.parentList[i].parentTitle;
            var imgWebUrl = encodeURI(videoInfo.parentList[i].parentImgWebUrl);
            var teacherId = videoInfo.parentList[i].teacherId;

            str += "<div class=\"col-md-4 col-sm-6  course\">";
            str += "<a class=\"course-box\" href=\"javascript:void(0);\" onclick=\"showChildVideoBefore(" + "'" + parenTitle + "'" + "," + "'" +teacherId + "'"+ ")\">";
            str += "<div class=\"sign-box\">";
            str += "<i class=\"fa fa-star-o course-follow pull-right\"";
            str += " data-follow-url=\"/courses/63/follow\"";
            str += " data-unfollow-url=\"/courses/63/unfollow\"  style=\"display:none\"  ></i>";
            str += "</div>";
            str += "<div class=\"course-img\">";
            str += "<img alt=\"" + parenTitle + "\" src=\"" + imgWebUrl + "\">";
            str += "</div>";
            str += "<div class=\"course-body\">";
            str += "<span class=\"course-title\" data-toggle=\"tooltip\" data-placement=\"bottom\" title=\"" + parenTitle + "\">" + parenTitle + "</span>";
            str += "</div>";
            str += "<div class=\"course-footer\">";
            str += "</div>";
            str += "</a>";
            str += "</div>";
        }
        document.getElementById("coursesInformation").innerHTML = str;
        document.getElementById("locationPageNumber").innerHTML = "当前页码为：" + locationCurrPage;
    }
}

function getSumVideoPageNumber() {
    if (currClass === "all") {
        var URL = "/getParentVideoSumNumber";
        $.ajax({
            url: URL,
            type: "GET",
            cache: false,
            async: false,
            success: function(data) {
                data = JSON.parse(data);
                var result = data.result;
                if (result !== "success") {
                    alert("无法获取视频总页数");
                }
                sumPageCount = data.sumNumber;
            },
            error: function() {
                alert("ajaxError: 函数getSumVideoPageNumber请求失败");
            }

        });
    } else {
        var URL = "/getParentVideoSumNumber?videoClass=" + currClass;
        $.ajax({
            url: URL,
            type: "GET",
            cache: false,
            async: false,
            success: function(data) {
                data = JSON.parse(data);
                var result = data.result;
                if (result !== "success") {
                    alert("无法获取视频总页数");
                }
                sumPageCount = data.sumNumber;
            },
            error: function() {
                alert("ajaxError: 函数getSumVideoPageNumber请求失败");
            }

        });
    }
}
function getSumChildVideoPageNumber(teacherId,parentTitle) {
    var URL = "/getChildVideoSumNumber?teacherId="+teacherId+"&parentTitle="+parentTitle;
    $.ajax({
        url: URL,
        type: "GET",
        cache: false,
        async: false,
        success: function(data) {
            data = JSON.parse(data);
            var result = data.result;
            if (result !== "success") {
                alert("无法获取视频总页数");
            }
            sumPageCount = data.sumNumber;
            showNumber();
        },
        error: function() {
            alert("ajaxError: 函数getSumVideoPageNumber请求失败");
        }

    });
}

function getClassVideoSumNumber(videoClass) {
    currClass = videoClass;
    if (videoClass === "C++") {
        currClass = "C%2B%2B";
    }
    child = false;
    getSumVideoPageNumber();
    showPage();
}
function isLogin() {
    $.ajax({
        url: "/get_user",
        type: "POST",
        cache: false,
        //设置不缓存
        success: changeLogin,
        error: function() {}
    });
}
function changeLogin(data) {
    if (data.indexOf("error:") !== -1) {

    } else {
        var user = JSON.parse(data);
        userName = user['name'];
        var str = "<ul class='nav navbar-nav'>" + "<li class='dropdown'>" + "<a href='javascript:void(0);' class='dropdown-toggle' data-toggle='dropdown'>" + "我的课程" + "</a>" + "<ul class='dropdown-menu'>" + "<li><a class='' href=''>" + data + "</a></li>" + "</ul>" + "</li>" + "</ul>" + "<ul class='nav navbar-nav'>" + "<li class='dropdown'>" + "<a href='javascript:void(0);' class='dropdown-toggle' data-toggle='dropdown'>" + "欢迎您，" + userName + "</a>" + "<ul class='dropdown-menu'>" + "<li><a class='' href='/home/index.html' >我的主页</a></li>" + "<li><a class='' href='/add_user/index.html' >添加用户</a></li>" + "<li><a class='' href='/back_stage/index.html' >后台管理</a></li>" + "<li><a class='' href='' onclick='signOut(); return false;' data-toggle='modal' data-target='#signOutModel'>退出登录</a></li>" + "</ul>" + "</li>" + "</ul>";
        document.getElementById("userInfo").innerHTML = str;
    }
}
function showChildVideoBefore(parentTitle, teacherId) {
    child=true;
    currParentTitle = parentTitle;
    currTeacherId = teacherId;
    getSumChildVideoPageNumber(teacherId,parentTitle);
    showChildVideo();
}
function showChildVideo() {
    var URL = "/getVideoByTeacherIdAndParentTitle?parentTitle=" + currParentTitle + "&teacherId=" + currTeacherId + "&pageNumber=" + locationCurrPage;
    $.ajax({
        url: URL,
        type: "GET",
        cache: false,
        async: false,
        success: showChildVideoSuccess,
        error: function() {
            alert("ajaxError: 函数getSumVideoPageNumber请求失败");
        }

    });
}
function showChildVideoSuccess(data) {
    var videoInfo = JSON.parse(data);
    var result = videoInfo.result;

    if (result === "error: 没有视频合集") {
        document.getElementById("coursesInformation").innerHTML = "该类视频暂无";
        document.getElementById("page-number").innerHTML = "";
        document.getElementById("locationPageNumber").innerHTML = "";
    } else {
        var videoNumber = videoInfo.videoList.length;
        var str = "";
        for (var i = 0; i < videoNumber; i++) {
            var childTile = videoInfo.videoList[i].childTitle;
            var childImgWebUrl = encodeURI(videoInfo.videoList[i].childImgWebUrl);
            var videoWebUrl = encodeURI(videoInfo.videoList[i].videoWebUrl);
            str += "<div class=\"col-md-4 col-sm-6  course\">";
            str += "<a class=\"course-box\" href=\"index.html?videoWebUrl=" + videoWebUrl+ "&imgWebUrl="+childImgWebUrl+"\">";
            str += "<div class=\"sign-box\">";
            str += "<i class=\"fa fa-star-o course-follow pull-right\"";
            str += " data-follow-url=\"/courses/63/follow\"";
            str += " data-unfollow-url=\"/courses/63/unfollow\"  style=\"display:none\"  ></i>";
            str += "</div>";
            str += "<div class=\"course-img\">";
            str += "<img alt=\"" + childTile + "\" src=\"" + childImgWebUrl + "\">";
            str += "</div>";
            str += "<div class=\"course-body\">";
            str += "<span class=\"course-title\" data-toggle=\"tooltip\" data-placement=\"bottom\" title=\"" + childTile + "\">" + childTile + "</span>";
            str += "</div>";
            str += "<div class=\"course-footer\">";
            str += "</div>";
            str += "</a>";
            str += "</div>";
        }
        document.getElementById("coursesInformation").innerHTML = str;
        document.getElementById("locationPageNumber").innerHTML = "当前页码为：" + locationCurrPage;
    }
}

$(document).ready(function() {
    isLogin();
    getSumVideoPageNumber();
    showPage();
});